#include<bits/stdc++.h>
using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	int a[n + 10][m + 10];
	for(int i = 1; i <= n; i ++ ) {
		for(int j = 1; j <= m; j ++ ) {
			cin >> a[i][j];
		}
	}
	int dp[n + 10][5010];
	memset(dp, 0, sizeof dp);
	dp[0][0] = 1;
	// 1   4    7
	for(int i = 1; i <= n; i ++ ) {
		for(int j = 1; j <= m; j ++ ) {
			// 1
			for(int k = a[i][j]; k <= 5000; k ++ ) {
				dp[i][k] = max(dp[i - 1][k - a[i][j]], dp[i][k]);
			}
		}
	} 
	int ret = 0x3f3f3f3f, x;
	cin >> x;
	for(int i = 1; i <= 5000; i ++ ) {
		if(dp[n][i]) {
			ret = min(ret, abs(x - i));
		}
	}
	cout << ret << endl;
	return 0;
}
/*
3 3
1 4 7
2 5 8
3 6 9
10

*/

